package gui;

import flexibility.CachedRowSetTableModel;
import flexibility.userData;
import guiCode.parentViewCode;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextPane;
import com.sun.rowset.CachedRowSetImpl;
import basic.ClientConsole;
import java.awt.SystemColor;
import java.sql.SQLException;
import javax.swing.DefaultComboBoxModel;
import secretary.closeSecretaryScreen;
import secretary.defineSemester;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class parentViewScreen  extends JFrame {
	
	private JTable table;
	private JComboBox studentComboBox;
	private JComboBox lowerComboBox;
	private JLabel lblStudentCoursesAndGrades;
	private JTextPane textPane_1 ;
	private JLabel lblStudentId;
	private JTextPane textPane;
	private JLabel lblClass;
	private JLabel lblPhoneNumber;
	private JScrollPane scrollPane;
	private JLabel lblStudentName;
	private JLabel lblInfo;
	private JLabel lblHeader;
	private parentViewCode pvc=new parentViewCode();
	private JTextPane textPanePhone;
	private userData userData= new userData();
	private closeSecretaryScreen close =new closeSecretaryScreen();
	private JButton btnInfo;
	private String studentName;
	private CachedRowSetImpl crs;
	private JButton btnShow;
	public defineSemester semester=new defineSemester();
	private JButton btnLogoutSystem;
	
	public parentViewScreen(ClientConsole conn, Integer whoAmI) {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		
		
		getContentPane().setBackground(SystemColor.inactiveCaption);
		setTitle("Parent View Screen");
		
		pvc.setCurrentSemester(semester.identify(0, conn, crs));
		
		pvc.setConnection(conn);
		pvc.setID(whoAmI);
		
	    SpringLayout springLayout = new SpringLayout();
		this.getContentPane().setLayout(springLayout);
		
		
		lblHeader = new JLabel(pvc.getParentName());
		
		springLayout.putConstraint(SpringLayout.NORTH, lblHeader, 10, SpringLayout.NORTH, getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, lblHeader, 46, SpringLayout.NORTH, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, lblHeader, -87, SpringLayout.EAST, getContentPane());
		lblHeader.setFont(new Font("Tahoma", Font.PLAIN, 30));
		getContentPane().add(lblHeader);
		
		lblInfo = new JLabel("You can watch student  file about:");
		springLayout.putConstraint(SpringLayout.NORTH, lblInfo, 29, SpringLayout.SOUTH, lblHeader);
		springLayout.putConstraint(SpringLayout.WEST, lblHeader, 0, SpringLayout.WEST, lblInfo);
		springLayout.putConstraint(SpringLayout.WEST, lblInfo, 32, SpringLayout.WEST, getContentPane());
		getContentPane().add(lblInfo);
		
		studentComboBox = new JComboBox();
		
		
		springLayout.putConstraint(SpringLayout.NORTH, studentComboBox, 37, SpringLayout.SOUTH, lblHeader);
		crs=pvc.getParentStudents();
		try {
			while(crs.next())
			{
				studentComboBox.addItem(crs.getString("studentID").concat("  ").concat(crs.getString("studentLastName")).concat(", ").concat(crs.getString("studentFirstName")));
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		springLayout.putConstraint(SpringLayout.WEST, studentComboBox, 239, SpringLayout.WEST, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, studentComboBox, -144, SpringLayout.EAST, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, lblInfo, -6, SpringLayout.WEST, studentComboBox);
		getContentPane().add(studentComboBox);
		
		lblStudentName = new JLabel("Student's personal Details:");
		springLayout.putConstraint(SpringLayout.SOUTH, lblInfo, -22, SpringLayout.NORTH, lblStudentName);
		springLayout.putConstraint(SpringLayout.NORTH, lblStudentName, 133, SpringLayout.NORTH, getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, lblStudentName, 32, SpringLayout.WEST, getContentPane());
		getContentPane().add(lblStudentName);
		
		scrollPane = new JScrollPane();
		springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 386, SpringLayout.NORTH, getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, lblHeader);
		springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, -109, SpringLayout.SOUTH, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, scrollPane, -37, SpringLayout.EAST, getContentPane());
		getContentPane().add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(new Object[][] {},new String[] {}));
		scrollPane.setViewportView(table);
		
		lblPhoneNumber = new JLabel("Phone number: ");
		springLayout.putConstraint(SpringLayout.NORTH, lblPhoneNumber, 70, SpringLayout.SOUTH, lblStudentName);
		springLayout.putConstraint(SpringLayout.WEST, lblPhoneNumber, 30, SpringLayout.WEST, getContentPane());
		getContentPane().add(lblPhoneNumber);
		
		lblClass = new JLabel("Current Class:");
		springLayout.putConstraint(SpringLayout.NORTH, lblClass, 27, SpringLayout.SOUTH, lblStudentName);
		springLayout.putConstraint(SpringLayout.WEST, lblClass, 32, SpringLayout.WEST, getContentPane());
		getContentPane().add(lblClass);
		
		textPane = new JTextPane();
		textPane.setEditable(false);
		textPane.setBackground(SystemColor.inactiveCaption);
		springLayout.putConstraint(SpringLayout.NORTH, textPane, 0, SpringLayout.NORTH, lblClass);
		springLayout.putConstraint(SpringLayout.WEST, textPane, 51, SpringLayout.EAST, lblClass);
		getContentPane().add(textPane);
		
		lblStudentId = new JLabel("Student ID");
		springLayout.putConstraint(SpringLayout.NORTH, lblStudentId, 20, SpringLayout.SOUTH, lblPhoneNumber);
		springLayout.putConstraint(SpringLayout.WEST, lblStudentId, 32, SpringLayout.WEST, getContentPane());
		getContentPane().add(lblStudentId);
		
		textPane_1 = new JTextPane();
		springLayout.putConstraint(SpringLayout.EAST, lblStudentId, -50, SpringLayout.WEST, textPane_1);
		springLayout.putConstraint(SpringLayout.EAST, textPane_1, 0, SpringLayout.EAST, textPane);
		textPane_1.setEditable(false);
		springLayout.putConstraint(SpringLayout.WEST, textPane_1, 152, SpringLayout.WEST, getContentPane());
		textPane_1.setBackground(SystemColor.inactiveCaption);
		getContentPane().add(textPane_1);
		
		lblStudentCoursesAndGrades = new JLabel("Student Courses And grades:");
		springLayout.putConstraint(SpringLayout.WEST, lblStudentCoursesAndGrades, 32, SpringLayout.WEST, getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, lblStudentCoursesAndGrades, -338, SpringLayout.SOUTH, getContentPane());
		getContentPane().add(lblStudentCoursesAndGrades);
		
		lowerComboBox = new JComboBox();
		springLayout.putConstraint(SpringLayout.NORTH, lowerComboBox, -3, SpringLayout.NORTH, lblStudentCoursesAndGrades);
		springLayout.putConstraint(SpringLayout.WEST, lowerComboBox, 8, SpringLayout.EAST, lblStudentCoursesAndGrades);
		
		
		lowerComboBox.setModel(new DefaultComboBoxModel(new String[] {"Current Semester Assigment", "Previous semesters"}));
		getContentPane().add(lowerComboBox);
		
		textPanePhone = new JTextPane();
		springLayout.putConstraint(SpringLayout.EAST, textPane, 0, SpringLayout.EAST, textPanePhone);
		springLayout.putConstraint(SpringLayout.WEST, textPanePhone, 46, SpringLayout.EAST, lblPhoneNumber);
		springLayout.putConstraint(SpringLayout.EAST, textPanePhone, -290, SpringLayout.EAST, getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, textPane_1, 14, SpringLayout.SOUTH, textPanePhone);
		textPanePhone.setEditable(false);
		textPanePhone.setBackground(SystemColor.inactiveCaption);
		springLayout.putConstraint(SpringLayout.NORTH, textPanePhone, 0, SpringLayout.NORTH, lblPhoneNumber);
		getContentPane().add(textPanePhone);
		
		btnInfo = new JButton("Info");
		springLayout.putConstraint(SpringLayout.NORTH, btnInfo, -4, SpringLayout.NORTH, lblStudentName);
		springLayout.putConstraint(SpringLayout.EAST, btnInfo, 0, SpringLayout.EAST, studentComboBox);
		getContentPane().add(btnInfo);
		
		btnShow = new JButton("Show");
		springLayout.putConstraint(SpringLayout.EAST, lowerComboBox, -18, SpringLayout.WEST, btnShow);
		springLayout.putConstraint(SpringLayout.NORTH, btnShow, -4, SpringLayout.NORTH, lblStudentCoursesAndGrades);
		springLayout.putConstraint(SpringLayout.WEST, btnShow, 95, SpringLayout.WEST, btnInfo);
		springLayout.putConstraint(SpringLayout.EAST, btnShow, 0, SpringLayout.EAST, scrollPane);
		getContentPane().add(btnShow);
		
		btnLogoutSystem = new JButton("Logout System");
		springLayout.putConstraint(SpringLayout.NORTH, btnLogoutSystem, -41, SpringLayout.SOUTH, getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, btnLogoutSystem, 32, SpringLayout.WEST, getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, btnLogoutSystem, -10, SpringLayout.SOUTH, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, btnLogoutSystem, 165, SpringLayout.WEST, getContentPane());
		getContentPane().add(btnLogoutSystem);
		dontShowComponantes();		
		setParentListeners();
		
		this.setSize(550,703);
		this.setLocation(100,10);
		this.setVisible(true);
		
		
		
	}
	/**
	 * Set Parent GUI its Listeners
	 */
	private void setParentListeners()
	{
		btnLogoutSystem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int n = JOptionPane.showConfirmDialog(
					    null,
					    "You are About Logout The System, are you sure you want to exit the program?",
					    "Log out",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
				if (n==JOptionPane.NO_OPTION ){
					return; 
				}
				close.closeSecretearyScreen(pvc.ParentIDId,pvc.getConnection(),userData);
			}
		});
		
		addWindowListener(new WindowAdapter() {
			
			@Override
			public void windowClosing(WindowEvent e) {
				int n = JOptionPane.showConfirmDialog(
					    null,
					    "You are About Logout The System, are you sure you want to exit the program?",
					    "Log out",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
				if (n==JOptionPane.NO_OPTION ){
					 return;
				}
				close.closeSecretearyScreen(pvc.ParentIDId,pvc.getConnection(),userData);
				
				
			}
		});
	
		studentComboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				dontShowComponantes();
			}
		});
		
		 
		btnInfo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 String s;
				 s=(String)studentComboBox.getSelectedItem();
				 System.out.print("\nShowBtn "+s+"\n");
				 s= s.substring(0, s.indexOf(" ")); //take StudentID from selected string 
				 System.out.print("\nShowBtn "+s+"\n");
				 
				 setSelectedStudentName(s);
				 
				if(!s.equals(null)){
					
				
					if(pvc.isStudentBlocked(s))
					{
						System.out.print("Student Blocked \n" );
						dontShowComponantes();						
						JOptionPane.showMessageDialog(null,"Your access to "+ (String)studentComboBox.getSelectedItem() +" was blocked by Manager.");
					}
					else{
					
						System.out.print("Student Not Blocked \n" );
						s=(String)pvc.getStudentDetail(s);
						textPanePhone.setText(s.substring(0,s.indexOf(" ")));
						textPane_1.setText((s=s.substring(s.indexOf(" ")+1)).substring(0,s.indexOf(" ")));
						textPane.setText((s=s.substring(s.indexOf(" ")+1)).substring(0,s.indexOf(" ")));
						setnotBlockedComponantes();
												
					 }
				 }
				
			 }
		 });
		 
		btnShow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(lowerComboBox.getSelectedItem().equals("Current Semester Assigment"))
				{
					table.setVisible(true);
					StudentAssigmentTable(getSelectedStudentName());///Set table of CurrentSemesterAssigments//
				}
				else if(lowerComboBox.getSelectedItem().equals("Previous semesters"))
				{
					table.setVisible(true);
					 System.out.print("Previous semesters\n"+getSelectedStudentName());
					crs= pvc.getStudentPreviousSemestersCoursegrades(getSelectedStudentName());//get DataBase table with Previous semesters data
					updateTable(parentViewScreen.this, crs);	
					
				}
			}
		});

	}
	
	public void setSelectedStudentName(String s){
		studentName=s;
	}
	
	public String getSelectedStudentName(){
		return studentName;
	}
	/**
	 * Update Parent Table according to Data Base data.
	 * @param rc receive frame that the table exist.
	 * @param crs DB table to show on table component.
	 */
	private void updateTable( parentViewScreen rc,CachedRowSetImpl crs ){
			try {
				rc.table.setModel(new CachedRowSetTableModel(crs));
				DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
				centerRenderer.setHorizontalAlignment(JLabel.CENTER);
				for (int i = 0; i < rc.table.getColumnCount(); i++)
					rc.table.getColumnModel().getColumn(i)
							.setCellRenderer(centerRenderer);
				
				} catch (SQLException e) {
				 e.printStackTrace();
			}
		}
	/**
	 * query DataBase for Student Assignment grade, CourseName,CourseID,Teacher.
	 * @param StudentID get Student ID witch assignment relevant.
	 */
	private void StudentAssigmentTable(String StudentID)
	{	
		System.out.print("StudentAssigmentTable(String StudentID): "+StudentID+"\n");
		pvc.createViews(StudentID);
		crs=pvc.getFinalTable();
		pvc.deleteViews();
		updateTable(parentViewScreen.this, crs);	
	
	}
	/**
	 * To Set visible all Components should be visible when parent is not block
	 */
	private void setnotBlockedComponantes()
	{
		scrollPane.setVisible(true);
		textPanePhone.setVisible(true);
		textPane_1.setVisible(true);
		textPane.setVisible(true);
		lblStudentId.setVisible(true);
		lblPhoneNumber.setVisible(true);
		lblClass.setVisible(true);
		lowerComboBox.setVisible(true);
		lblStudentName.setVisible(true);
		btnShow.setEnabled(true);
	}
	/**
	 * To Set visible all Components should not be visible when parent is  block or for the first time GUI was up.
	 */
	private void dontShowComponantes()
	{
		textPanePhone.setVisible(false);
		textPane_1.setVisible(false);
		textPane.setVisible(false);
		scrollPane.setVisible(false);
		lblStudentId.setVisible(false);
		lblPhoneNumber.setVisible(false);
		lblClass.setVisible(false);
		lowerComboBox.setVisible(false);
		lblStudentName.setVisible(false);
		btnShow.setEnabled(false);
	}
}
